//
//  String+Extension.swift
//  Z来源的处理
//
//  Created by 张洪毓 on 15/11/22.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import Foundation

extension String {
    
    func linhref() -> (link: String?,clinetName: String?) {
        //"dgfhghgjhgj<a href=\"http://app.weibo.com/t/feed/9ksdit\" rel=\"nofollow\">iPhone客户端</a>hghjgjhghjgh"
        
        //设置匹配方案
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        //定义正则表达式：如果需要抛出错误，可以强try
        let regx = try! NSRegularExpression(pattern: pattern, options: [])
        //返回值
        let result = regx.firstMatchInString(self, options: [], range: NSRange(location: 0, length: characters.count))
//        print(result?.numberOfRanges)
//        print(result?.rangeAtIndex(0))
//        print(result?.rangeAtIndex(1))
//        print(result?.rangeAtIndex(2))


        let range2 = result?.rangeAtIndex(1)
        let range3 = result?.rangeAtIndex(2)

        var subStr2: String?
        var subStr3: String?
        if let r2 = range2,r3 = range3 {

            subStr2 = (self as NSString).substringWithRange(r2)
            subStr3 = (self as NSString).substringWithRange(r3)
        }
        return (subStr2,subStr3)
    }
}